package org.example.ag_notes.dto.article;
import lombok.Builder;
import lombok.Data;
import org.example.ag_notes.entity.TreeNode;
import org.example.ag_notes.entity.enums.NodeType;

import java.util.ArrayList;
import java.util.List;

@Data
public class TreeNodeDto {
    private Integer id;
    private String label;
    private NodeType type;
    private Integer parentId;  // 父节点ID，根节点为null
    private Integer articleId; // 仅当type=ARTICLE时有值
    private List<TreeNodeDto> children = new ArrayList<>();  // 子节点列表


    // 构造方法
    public TreeNodeDto(Integer id, String label, NodeType type,
                       Integer parentId, Integer articleId) {
        this.id = id;
        this.label = label;
        this.type = type;
        this.parentId = parentId;
        this.articleId = articleId;
        this.children = new ArrayList<>();
    }

    public TreeNodeDto() {

    }

    public static TreeNodeDto fromEntity(TreeNode node) {
        if (node == null) {
            return null;
        }
        TreeNodeDto dto = new TreeNodeDto();
        dto.setId(node.getId());
        dto.setLabel(node.getLabel());
        dto.setType(node.getType());
        dto.setParentId(node.getParentNode() != null ? node.getParentNode().getId() : null);
        dto.setArticleId(node.getArticle() != null ? node.getArticle().getId() : null);
        // 注意：children 一般在组装树结构时另行设置
        return dto;
    }
}

